package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.service.ICompanyService;
import com.cn.wanxi.util.IO;
import com.cn.wanxi.util.Path;
import com.cn.wanxi.util.PathEnum;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 1.要创建文件  必定给一个路径 和一个文件名
 * 2.写入数据
 * 3.读取数据
 * 4.封装数据
 */
public class CompanyServiceImpl implements ICompanyService {


    @Override
    public CompanyModel getCompanyModel() {
        CompanyModel model = new CompanyModel();
//        1.创建文件
//        File file = IO.createNote(Path.PATH, Path.TEMP, Path.FILE_NAME_COMPANY);
//        1.5  装dao层数据
        List list = getCompanyDaoList();
        //        2.写入数据
        IO.writeNote(list, PathEnum.FILE_NAME_COMPANY.getValue());
//        3.读取数据
        String result = IO.readNote();
//        4.封装数据
        model = setModel(result);
        return model;
    }

    private List<String> getCompanyDaoList() {
        List<String> list = new ArrayList<>();
        CompanyDao companyDao = new CompanyDao();
        list.add(companyDao.getLogo());
        list.add(companyDao.getPhone());
        list.add(companyDao.getNet());
        list.add(companyDao.getEmail());
        list.add(companyDao.getAddress());
        list.add(companyDao.getLinePhone());
        list.add(companyDao.getVersion());
        return list;

    }

    /**
     * @param result
     * @return
     */
    private CompanyModel setModel(String result) {
        String[] results = result.split(PathEnum.SPLIT.getValue());
//        得到companyModel的对象
//        封装数据
        CompanyModel model = new CompanyModel();
        model.setLogo(results[0]);
        model.setPhone(results[1]);
        model.setNet(results[2]);
        model.setEmail(results[3]);
        model.setLinePhone(results[4]);
        model.setAddress(results[5]);
        model.setVersion(results[6]);
        return model;
    }

}
